#include"GTcpServer.h"

#include<libesmtp.h>
#include<auth-client.h>

GTcpServer::GTcpServer(QObject* parent) : QTcpServer(parent) {
	/*
	//create the session
	smtp_session_t ses = smtp_create_session();
	
	//setup the server we are connecting to
	smtp_set_server(ses, "smtp.rutgers.edu");
	
	//enable tls
	smtp_starttls_enable(ses, Starttls_ENABLED);
	
	//setup authentication
	auth_context_t auth = auth_create_context();
	
	
	
	
	//add a message to the session
	smtp_message_t message = smtp_add_message(ses);
	
	
	char errmsg[200];
	smtp_strerror(smtp_errno(), errmsg, 200);
	
	cout<<"err: " <<smtp_errno() <<" " <<errmsg <<endl;
	auth_destroy_context(auth);
	smtp_destroy_session(ses);
	exit(21);
	*/
	//setup login thread
	logins = new GLogin(this);
	logins->initialize();
	connect(this, SIGNAL(closeDown()), logins, SLOT(shutdown()));
	
}

GTcpServer::~GTcpServer() {
	
}

void GTcpServer::incomingConnection(int sfd) {
	cout<<"incoming connection" <<endl;
	GUser* temp = new GUser(this, sfd);
	
	//connection for logging account
	connect(temp, SIGNAL(loginAccount(GUser*,QString,QString)), logins, 
			SLOT(login(GUser*,QString,QString)));
	//connection for logging out account
	connect(temp, SIGNAL(logoutAccount(GUser*)), logins, SLOT(logout(GUser*)));
	//connection for creating a new account
	connect(temp, SIGNAL(addAccount(QString,QString,QString)), logins, 
			SLOT(add(QString,QString,QString)));
	
	//setup connections for the status feature in the freinds list
	connect(temp, SIGNAL(linkFriendList(GUser*,GFriend*,int,QString)),
					logins, SLOT(linkFriendList(GUser*,GFriend*,int,QString)));
	
	//create messaging channel initialization call
	connect(temp, SIGNAL(setupMessageChannel(GChatMap*,QString)), logins, 
			SLOT(createMessageChannel(GChatMap*,QString)));
	
	//create file tranfer initialization call
	connect(temp, SIGNAL(setupFileTransferChannel(GFileMap*,GUser*,QString)), logins,
			SLOT(createFileTransferChannel(GFileMap*,GUser*,QString)));
	
	//create connection so a new account may be verified
	connect(temp, SIGNAL(setAccountVerified(QString)), logins,
			SLOT(setAccountVerified(QString)));
	
}

void GTcpServer::signalQuit() {
	emit closeDown();
}
